﻿//
// Created by Grishul Eugeny
//
// See license in License.txt
//

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using NCommander.Core;

namespace NCommander
{
	public partial class FilesView : NodeView
	{
		public IIconProvider IconProvider { get; set; }

		public FilesView()
		{
			InitializeComponent();
		}

		public override void OnProviderRefresh( object provider, EventArgs args )
		{
			if( NodeProvider != null )
			{
				try
				{
					var shellIcons = new ImageList();

					_nodes.BeginUpdate();
					_nodes.Items.Clear();
					_nodes.LargeImageList = shellIcons;
					_nodes.SmallImageList = shellIcons;

					var nodes = NodeProvider.GetNodes();

					foreach( var directory in nodes.Where( x => x.Type == NodeType.Internal ) )
					{
						var result = new ListViewItem( "[" + Path.GetFileName( directory.Path ) + "]" );
						result.Tag = directory;

						var icon = IconProvider.GetIcon( directory.Path );
						if( icon != null )
						{
							result.ImageIndex = shellIcons.Images.Count;
							shellIcons.Images.Add( icon );
						}

						_nodes.Items.Add( result );
					}

					foreach( var file in nodes.Where( x => x.Type == NodeType.External ) )
					{
						var result = new ListViewItem( Path.GetFileName( file.Path ) );
						result.Tag = file;

						var icon = IconProvider.GetIcon( file.Path );
						if( icon != null )
						{
							result.ImageIndex = shellIcons.Images.Count;
							shellIcons.Images.Add( icon );
						}

						_nodes.Items.Add( result );
					}


					_recentDirectories.Text = NodeProvider.CurrentNode.Path;
					DirectoryNavigated();
				}
				finally
				{
					_nodes.EndUpdate();
				}
			}
		}

		public void SelectAll()
		{
			foreach( ListViewItem item in _nodes.Items )
			{
				if( item.Tag is FollowUpNode )
					continue;

				item.Selected = true;
			}
		}
	}
}
